<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>添加课程</title>
    <link rel="stylesheet/less" href="../../../static/css/index.less"/>
    <link rel="stylesheet" href="../../../static/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../../static/css/jquery.pagination.css"/>
    <script src="../../../static/js/jquery-1.12.3.js"></script>
    <script src="../../../static/js/ajaxfileupload.js"></script>
    <script src="../../../static/js/less.min.js"></script>
    <script src="../../../static/layui/layui.js"></script>
    <script src="../../../static/layer/layer.js"></script>
    <script type="text/javascript" charset="utf-8" src="../../../static/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../../../static/ueditor/ueditor.all.min.js"> </script>
    <script type="text/javascript" charset="utf-8" src="../../../static/ueditor/lang/zh-cn/zh-cn.js"></script>
</head>
<style>
    .newadd {
        padding: 10px 0;
        display: block;
        text-align: left;
    }

    .newadd label .adtitle {
        display: inline-block;
        width: 6rem;
        text-align: right;
        padding-right: 2%;
    }

    .newadd input {
        padding: 0 10px;
        width: 20%;
        height: 30px;
        line-height: 30px;
        border: 1px solid #dcdcdc;
        border-radius: 5px;
    }

    .newadd select {
        border: 1px solid #dcdcdc;
        border-radius: 5px;
    }

    .button button {
        color: #fff;
        padding: 5px 10px;
        -webkit-border-radius: 3px;
        -moz-border-radius: 3px;
        border-radius: 3px;
    }

    .button button a {
        color: #fff;
    }
</style>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    {% include "admin/top.html" %}
    {% include "admin/left.html" %}
    <div class="layui-body">
        <div id="box" class="fl" style="width: 100%">
            <div class="content">
                <h3>添加课程</h3>
                <div class="newadd">
                    <label for="">
                        <span class="adtitle">选择教师</span>
                        <select name="" id="tc">
                            <option value="0">选择教师</option>
                            {% if tc %}
                            {% for d in tc %}
                            <option value="{{ d.uid }}">{{ d.nickName }}</option>
                            {% endfor %}
                            {% endif %}
                        </select>
                    </label>

                    <label for="">
                        <span class="adtitle">选择科目</span>
                        <select name="" id="km">
                            <option value="0">请选择</option>
                            {% if sj %}
                            {% for d in sj %}
                            <option value="{{ d.sid }}">{{ d.typeName }}</option>
                            {% endfor %}
                            {% endif %}
                        </select>
                    </label>

                    <label for="">
                        <span class="adtitle">选择科目类型</span>
                        <select name="" id="ctype">
                            <option value="0">请选择</option>
                            {% if type_list %}
                            {% for d in type_list %}
                            <option value="{{ d.tid }}">{{ d.courseName }}</option>
                            {% endfor %}
                            {% endif %}
                        </select>
                    </label>

                    <label>
                        <span class="adtitle">课程名称</span>
                        <input type="text" id="cname" class="create" value="">
                    </label>

                    <label>
                        <span class="adtitle">课程描述</span>
                        <div style="margin-left: 7.8rem;"><script id="editor" type="text/plain" style="width:710px;height:150px;"></script></div>
                    </label>

                    <label>
                        <span class="adtitle">课程封面图</span>
                        <input type="file" name="file" id="xdaTanFileImg">
                    </label>

                    <label class="button" style="margin-left: 8rem;">
                        <button id="btn" class="subbtn">提交</button>
                        <button class="subbtn">
                            <a href="javascript:window.history.go(-1)">返回课程列表</a>
                        </button>
                    </label>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script>
    var ue = UE.getEditor('editor');
    function getContent() {
        var arr = [];
        arr.push(UE.getEditor('editor').getContent());
        alert(arr.join("\n"));
        return arr.join("")
    }
    $("#km").change(function () {
        $.ajax({
            url: "/admin/subject_detail",
            type: "POST",
            data: {
                sid: $("#km").val()
            },
            success: function (msg) {
                var q = '<option value="0">请选择</option>';
                if (msg.status == 1){
                    for (var i = 0; i < msg.res.length; i++){
                        q += '<option value="' + msg.res[i].tid + '">' + msg.res[i].courseName + '</option>';
                    }

                }else {
                }
                $('#ctype').html(q);
            }
        })
    });
    $("#btn").click(function () {
        var tc = $('#tc').val();
        var km = $('#km').val();
        var cname = $('#cname').val();
        var info = getContent();
        var ctype = $('#ctype').val();
        if (tc == 0){
            layer.msg("请选择教师");
            return false;
        }
        if (km == 0){
            layer.msg("请选择科目");
            return false;
        }
        if (ctype == 0){
            layer.msg("请选择科目类型");
            return false;
        }
        if (cname == ''){
            layer.msg("请填写课程名称");
            return false;
        }
        if (info == ''){
            layer.msg("请填写课程描述");
            return false;
        }
        $.ajaxFileUpload({
            url: "/admin/course_add_post",
            type: "POST",
            dataType: 'json',
            data: {
                "tc":tc,
                "km":km,
                "cname":cname,
                "info":info,
                "ctype":ctype
            },
            secureuri: false, //是否需要安全协议，一般设置为false
            fileElementId: 'xdaTanFileImg', //文件上传域的ID
            success: function (msg) {
                if (msg.status == 1) {
                    layer.msg("新增成功", {time: 2000}, function () {
                        location.href = window.history.go(-1);
                    })
                } else {
                    layer.msg("操作失败");
                }
            }
        })
    })
</script>



